<template>
	<view class="photoBox">
		<view class="addBox" @click="see(item)" v-for="(item,index) in uploadedPhoto" :key="index">
			<image class="addImage" :src="item.filePath"></image>
			<image @click.native.stop="deleteImage(index)" class="commonSingleBoxImage" src="../static/cyry.png"></image>
		</view>
		<view class="addBox" @click="chosePhoto" v-if="uploadedPhoto &&  uploadedPhoto.length == 0">
			<image class="image" src="../static/tjfh.png"></image>
		</view>
	</view>
</template>

<script>
	import {
		getToken
	} from '../api/byfz';
	import {
		config
	} from '../config/config';
	import {
		getMessionId, preView
	} from '../utils/util';
	export default {
		name: "photoUpload",
		props: {},
		data() {
			return {
				uploadedPhoto: []
			};
		},
		methods: {
			deleteImage(index){
				let newList = [];
				for(let i=0;i<this.uploadedPhoto.length;i++){
					if(i!==index){
						newList.push(this.uploadedPhoto[i])
					}
				}
				this.uploadedPhoto = newList;
			},
			see(item){
				preView(item.filePath,'image',item.name)
			},
			getList(){
				return this.uploadedPhoto
			},
			uploadImage(filePath){
				uni.uploadFile({
					url: config.apiBaseUrl + 'common/upload/',
					filePath: filePath,
					name: 'file',
					fileType: "image", //ZFB必填,不然报错
					header: {
						Authorization: getToken()
					},
					success: (result) => {
						const obj = JSON.parse(result.data);
						if (obj.code === 200) {
							console.log("obj==",obj);
							this.uploadedPhoto.push({
								filePath: obj.url,
							})
							// this.$emit("uploadSuccess",obj.msg)
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络异常，上传失败!',
							icon: 'error'
						})
					},
				});
			},
			chosePhoto() {
				uni.chooseImage({
					count: 4,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera', 'album'], //这要注意，camera掉拍照，album是打开手机相册
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						for(let item of tempFilePaths){
							this.uploadImage(item)
						}
					},
					file:(res)=>{
						uni.showToast({
							title: '网络异常，上传失败!',
							icon: 'error'
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.photoBox {
		display: flex;
		flex-wrap: wrap;
		padding-right: 20rpx;
		padding-left: 40rpx;
	}

	.addBox {
		width: 151rpx;
		height: 151rpx;
		background: #DADADA;
		border-radius: 7rpx 7rpx 7rpx 7rpx;
		opacity: 1;
		border: 0rpx solid #FFFFFF;
		position: relative;
		z-index: 1;
		margin-right: 20rpx;
		.addImage{
			width: 151rpx;
			height: 151rpx;
		}

		.commonSingleBoxImage {
			position: absolute;
			right: -18rpx;
			top: -18rpx;
			width: 37rpx;
			height: 37rpx;
			background: #C9C9C9;
			border-radius: 37rpx;
			opacity: 1;
			border: 0rpx solid #FFFFFF;
		}

		.image {
			position: absolute;
			left: 50rpx;
			right: 50rpx;
			top: 50rpx;
			bottom: 50rpx;
			width: 51rpx;
			height: 51rpx;
			z-index: 2;
		}
	}
</style>